# 类
class Person():
    def __init__(self, age, name):
        self.age = age
        self.name = name
p = Person(23, 'ali')
p.sex = 'male'
q = Person(32, 'baba')
print(p.name, p.age, p.sex)
# print(q.name, q.age, q.sex) # AttributeError: 'Person' object has no attribute 'sex'


# 访问修饰符
class Man():
    def __init__(self, age, name, weight):
        self.__age = age
        self.__name = name
        self.__weight__ = weight
man = Man(32, 'ali', 190)
print(man.__weight__, man._Man__age) # 这里强制访问私有变量方式



# 继承
class Parent():
    def live(self):
        print('live')
    def breath(self):
        print('breath')


class Son(Parent):
    def live(self):
        print('live son')

# 继承父类的成员函数以及变量
son = Son()
son.live()
son.breath()

# 通过isinstance判断是否是类实例
print(isinstance(son, Parent))

# 鸭子 模型
def breath(anim):
    anim.breath()
breath(son)


# 获取对象信息
class Person():
    def __init__(self, sex):
        self.sex = sex

p = Person('male')
print(type(p))
print(isinstance(p, Person))
ds = p.__dir__()
print(ds)   # ['sex', '__module__', '__init__', '__dict__', '__weakref__', '__doc__'...

print(hasattr(p, 'sex'))
setattr(p, 'sex', 'female')
print(getattr(p, 'sex', 'unknow'))

print('#' * 199)
# 类属性
class Person():
    name = 'Person'
    def __init__(self, age):
        self.age = age
p = Person(32)
print(p.name)
p.name = 'jack'
print(p.name)
print(Person.name)